package com.towker.frame.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.towker.core.util.RefGoodsType;
import com.towker.core.util.SpringNumberEditor;
import com.towker.core.util.StringUtil;
import com.towker.frame.model.sell.RefGoods;
import com.towker.frame.service.basicData.SurplusOrderService;
import com.towker.frame.service.sell.RefGoodsService;
import com.towker.frame.service.warehouse.WarehouseAccountsService;
import com.towker.web.MyRequest;

public abstract class BaseController {
	protected RefGoodsService refGoodsService;

	@InitBinder
	public void initBinder(WebDataBinder webDataBinder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(
				sdf, true));
		webDataBinder.registerCustomEditor(Double.class, new SpringNumberEditor(Double.class));
		webDataBinder.registerCustomEditor(Integer.class, new SpringNumberEditor(Integer.class));
		webDataBinder.registerCustomEditor(Long.class, new SpringNumberEditor(Long.class));
	}

	public void setRefGoodsService(RefGoodsService refGoodsService) {
		this.refGoodsService = refGoodsService;
	}
	
	/**
	 * 更新物料库存
	 * 
	 * @param oldRefGoods
	 *            更改前物料
	 * @param newRefGoods
	 *            更改后物料
	 * @param surplusOrderService
	 * @param depotCode
	 *            仓库代码
	 * @param add
	 *            是否为增加库存
	 * @param updateGoods
	 *            是否更新物料总库存
	 */
	protected void calcGoodsStock(List<RefGoods> oldRefGoods, List<RefGoods> newRefGoods, SurplusOrderService surplusOrderService, String depotCode, String coCode, boolean add, boolean updateGoods) {
		List<RefGoods> goods = new ArrayList<RefGoods>();
		for (RefGoods n : newRefGoods) {
			RefGoods o = getRefGoodByGoodsId(oldRefGoods, n.getGoods_id());
			if (o == null) {
				goods.add(n);
			} else if (o.getAmount() != n.getAmount()) {
				RefGoods rg = new RefGoods();
				rg.setGoods_id(o.getGoods_id());
				rg.setAmount(n.getAmount() -  o.getAmount());
				goods.add(rg);
			}
		}
		for (RefGoods o : oldRefGoods) {
			RefGoods n = getRefGoodByGoodsId(newRefGoods, o.getGoods_id());
			if (n == null) {
				RefGoods rg = new RefGoods();
				rg.setGoods_id(o.getGoods_id());
				rg.setAmount(o.getAmount());
				goods.add(rg);
			}
		}
		for (RefGoods rg : goods) {
			surplusOrderService.addStockAmount(rg.getGoods_id(), depotCode, coCode, add?rg.getAmount() : -rg.getAmount(), updateGoods);
		}
	}
	
	protected RefGoods getRefGoodByGoodsId(List<RefGoods> refGoods, int goodsId) {
		for (RefGoods r : refGoods) {
			if (r.getGoods_id() == goodsId) {
				return r;
			}
		}
		return null;
	}

	/**
	 * 处理各种关联物品
	 * @param request
	 * @param refGoodsType
	 * @param infoId
	 * @param goodsInfo
	 */
	protected List<RefGoods> processRefGoods(HttpServletRequest request,	RefGoodsType refGoodsType, Integer infoId, String goodsInfo) {
		refGoodsService.deleteByInfoType(refGoodsType.getValue(), infoId);
		List<RefGoods> list = new ArrayList<RefGoods>();
		MyRequest myRequest = new MyRequest(request);
		String[] gIds = goodsInfo.split(",");
		for (String gid : gIds) {
			if (StringUtils.isNotBlank(gid)) {
				RefGoods refGoods = new RefGoods();
				refGoods.setAmount(StringUtil.toDouble(myRequest.getString("amount_" + gid,"0")));
				refGoods.setInfo_type(refGoodsType.getValue());
				refGoods.setInfo_id(infoId);
				refGoods.setGoods_id(StringUtil.toInteger(gid));
				refGoods.setDiscount(myRequest.getInt("discount_" + gid));
				refGoods.setWprice(StringUtil.toDouble(myRequest	.getString("wprice_" + gid,"0")));
				refGoods.setSprice(StringUtil.toDouble(myRequest.getString("sprice_" + gid,"0")));
				refGoods.setSumMoney(StringUtil.toDouble(myRequest.getString("sumMoney_" + gid,"0")));
				refGoodsService.add(refGoods);
				list.add(refGoods);
			}
		}
		return list;
	}

	/**
	 * 记录仓库账
	 * 
	 * @param service
	 * @param refGoodsType
	 * @param infoId
	 * @param depotCode
	 * @param add
	 */
	protected void calcWarehouseAccount(WarehouseAccountsService service, RefGoodsType refGoodsType, Integer infoId, String depotCode, String coCode, boolean add) {
		List<RefGoods> refGoods = refGoodsService.getByInfoType(refGoodsType.getValue(), infoId);
		for (RefGoods rg : refGoods) {
			service.addStockAmount(rg.getGoods_sn(), depotCode, coCode, add ? rg.getAmount() : -rg.getAmount());
		}
	}
}
